import 'dart:convert';
import 'dart:io';

import 'package:convert/convert.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mode/utils.dart';

/// kiven 2021/8/24
class ConvertDemo extends StatelessWidget {
  final codecs = <A>[
    // String -> List<int>
    A('ascii', 'String', 'List<int>', () => ascii),
    A('utf8', 'String', 'List<int>', () => utf8),
    A('systemEncoding', 'String', 'List<int>', () => systemEncoding),
    // latin, latin1与其他latin在不同库里面，其他latin是在补充库里面。
    A('latinCyrillic', 'String', 'List<int>', () => latinCyrillic),
    A('latinArabic', 'String', 'List<int>', () => latinArabic),
    A('latinGreek', 'String', 'List<int>', () => latinGreek),
    A('latinHebrew', 'String', 'List<int>', () => latinHebrew),
    A('latin1', 'String', 'List<int>', () => latin1),
    A('latin2', 'String', 'List<int>', () => latin2),
    A('latin3', 'String', 'List<int>', () => latin3),
    A('latin4', 'String', 'List<int>', () => latin4),
    A('latin5', 'String', 'List<int>', () => latin5),
    A('latin6', 'String', 'List<int>', () => latin6),
    A('latin7', 'String', 'List<int>', () => latin7),
    A('latin8', 'String', 'List<int>', () => latin8),
    A('latin9', 'String', 'List<int>', () => latin9),
    A('latin10', 'String', 'List<int>', () => latin10),

    // List<int> -> String
    A('base64', 'List<int>', 'String', () => base64),
    // hex/percent 来自补充库
    A('hex', 'List<int>', 'String', () => hex),
    A('percent', 'List<int>', 'String', () => percent),

    // 其他
    A('json', 'Object', 'String', () => json),
    A('zlib', 'List<int>', 'List<int>', () => zlib),
    A('gzip', 'List<int>', 'List<int>', () => gzip),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('ConvertDemo'),
          centerTitle: true,
        ),
        body: SingleChildScrollView(
          child: Wrap(
            spacing: 8,
            runSpacing: 4,
            children: codecs.map((e) => addAction(e.name, () async {
              final subs = codecs.where((element) => e.toB == element.fromA);

              var a = <String>[e.name];
              a.addAll(subs.map((f) => '${e.name}.fuse(${f.name})'));

              if (a.length == 1) {
                _codec(context, e.getCodec(), e.fromA);
                return;
              }

              final r = await showMyBottomSheet(context, a);
              if (r < 0) return;

              if (r == 0) {
                _codec(context, e.getCodec(), e.fromA);
              } else {
                _codec(context, e.getCodec().fuse(subs.toList()[r-1].getCodec()), e.fromA);
              }
            })).toList(),
          ),
        ));
  }

  _codec(BuildContext context, Codec codec, String fromType) async {
    final ss = ['ABC 123 @!( +-_*=, without chinese', 'ABC 123 @!(  +-_*=, 有中文'];
    var s;
    if (fromType == 'String') {
      final r = await showMyBottomSheet(context, ss);
      if (r < 0) return;
      s = ss[r];
    } else if (fromType == 'List<int>') {
      s = utf8.encode(ss[1]);
    } else if (fromType == 'Object') {
      s = [
        {"name": "Kivo,", "age": 7},
        {"name": "大宝", "age": 17}
      ];
    }
    if (s == null) return;

    final r = codec.encode(s);
    print('编码: $r');
    print('解码: ${codec.decode(r)}');
  }

  Widget addTitle(String title) {
    return Center(
      child: Text(title),
    );
  }

  Widget addAction(String title, Function() action) {
    return ElevatedButton(
      onPressed: action,
      child: Text(title),
    );
  }
}

class A {
  final String name;
  final String fromA;
  final String toB;
  final Codec Function() getCodec;

  A(this.name, this.fromA, this.toB, this.getCodec);


}